///新版本
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/NewMine/AccountViewModel.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';

import '../../../base/basewidget/view/BaseWidget.dart';

class AccountDetailView extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return AccountDetailState();
  }
}

class AccountDetailState
    extends BaseState<AccountViewModel, AccountDetailView> {
  var _data;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  _getData() {
    viewModel.getUserInfo();
    viewModel.getUserInfoDataStream().listen((event) {
      if (event['code'] == '0') {
        setState(() {
          _data = event['data'];
        });
      } else {
        ToastUtils.showToast(event['msg']);
      }
    }, onError: (e) {
      print(e);
    });
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return new Scaffold(
      backgroundColor: SQColor.theme_Bg_color,
      appBar: new AppBar(
        centerTitle: true,
        elevation: 0,
        backgroundColor: SQColor.theme_color,
        title: JText(
          '账号与安全',
          color: SQColor.white,
          fontSize: 18,
        ),
      ),
      body: _data == null ? LoadingWidget() : _bodyView(),
    );
  }

  _bodyView() {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[_topContent(), _listContent()],
      ),
    );
  }

  _topContent() {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(left: Dimens.dimens16, top: Dimens.dimens14),
          child: Row(
            children: [
              JText(
                '账号信息',
                color: SQColor.black_333333,
                fontSize: 14,
                fontWeight: FontWeight.w500,
              ),
            ],
          ),
        ),
        SizedBox(
          height: 8,
        ),
        Container(
          margin:
              EdgeInsets.only(left: Dimens.dimens14, right: Dimens.dimens14),
          decoration: BoxDecoration(
            color: SQColor.white,
            borderRadius: BorderRadius.all(Radius.circular(4.0)),
          ),
          child: Column(
            children: [
              _itemTopcontent("手机号"),
              Container(
                height: 1.0,
                color: SQColor.lightGray1,
                margin: EdgeInsets.only(
                    left: Dimens.dimens16, right: Dimens.dimens16),
              ),
              _itemTopcontent("身份证号"),
            ],
          ),
        )
      ],
    );
  }

  _itemTopcontent(String content) {
    return InkWell(
      child: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Container(
                  height: 59,
                  padding: EdgeInsets.only(left: Dimens.dimens16),
                  child: Row(
                    children: [
                      JText(
                        content,
                        color: SQColor.black_333333,
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                      ),
                    ],
                  ),
                ),
              ],
            ),
            Container(
                padding: EdgeInsets.only(right: Dimens.dimens18),
                child: JText(
                  content == '身份证号' ? _data['idCard'] ?? '' : '${_data['phone'].toString().substring(0,3)}****${_data['phone'].toString().substring(7,11)}',
                  color: SQColor.black_999999,
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                )),
          ],
        ),
        height: 59,
      ),
      onTap: () {},
    );
  }

  _listContent() {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(left: Dimens.dimens16, top: Dimens.dimens14),
          child: Row(
            children: [
              JText(
                '安全中心',
                color: SQColor.black_333333,
                fontSize: 14,
                fontWeight: FontWeight.w500,
              ),
            ],
          ),
        ),
        SizedBox(
          height: 8,
        ),
        Container(
          margin:
              EdgeInsets.only(left: Dimens.dimens14, right: Dimens.dimens14),
          decoration: BoxDecoration(
            color: SQColor.white,
            borderRadius: BorderRadius.all(Radius.circular(4.0)),
          ),
          child: Column(
            children: [
              _itemcontent("修改密码"),
              Container(
                height: 1.0,
                color: SQColor.lightGray1,
                margin: EdgeInsets.only(
                    left: Dimens.dimens16, right: Dimens.dimens16),
              ),
              _itemcontent("忘记密码"),
            ],
          ),
        )
      ],
    );
  }

  _itemcontent(String content) {
    return InkWell(
      child: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Container(
                  height: 59,
                  padding: EdgeInsets.only(left: Dimens.dimens16),
                  child: Row(
                    children: [
                      JText(
                        content,
                        color: SQColor.black_333333,
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                      ),
                    ],
                  ),
                ),
              ],
            ),
            Container(
                padding: EdgeInsets.only(right: Dimens.dimens18),
                child: Center(
                  child: Icon(
                    Icons.arrow_forward_ios_outlined,
                    size: Dimens.dimens15,
                    color: SQColor.black_999999,
                  ),
                )),
          ],
        ),
        height: 59,
      ),
      onTap: () {
        if (content == "修改密码") {
          Navigator.pushNamed(context, "/ChangePswdView");
        } else {
          Navigator.pushNamed(context, "/SureYouView");
        }
      },
    );
  }
}
